The goal of this lab is to play around with the theme options in ggplot2.
Datasets
We’ll be using the cdc.txt and NU_admission_data.csv datasets.
Code
# load package(s)library(tidyverse)library(janitor)library(patchwork)library(showtext)# read in the cdc datasetcdc <-read_delim(file ="data/cdc.txt", delim ="|") %>%mutate(genhlth =factor( genhlth,levels =c("excellent", "very good", "good", "fair", "poor"),labels =c("Excellent", "Very Good", "Good", "Fair", "Poor") ) )# read in NU admission dataadmin_data <-read.csv("data/NU_admission_data.csv") %>%clean_names()# set seedset.seed(2468)# selecting a random subset of size 100cdc_small <- cdc %>%slice_sample(n =100)
Exercise 1
Use the cdc_small dataset to explore several pre-set ggthemes. The code below constructs the familiar scatterplot of weight by height and stores it in plot_01. Display plot_01 to observe the default theme. Explore/apply, and display at least 7 other pre-set themes from the ggplot2 or ggthemes package. Don’t worry about making adjustments to the figures under the new themes. Just get a sense of what the themes are doing to the original figure plot_01.
There should be at least 8 plots for this task, plot_01 is pictured below. Use patchwork or cowplot in combination with R yaml chunk options fig-height and fig-width (out-width and fig-align may be useful as well) to setup the 8 plots together in a user friendly arrangement.
Solution
Important
Which theme or themes do you particularly like? Why?
Solution
I personally like theme_grey(), and theme_classic(). For a visualization like this, I think the grey background provides a helpful contrast to the colored shapes while alos clearly outlining the legend in the bottom right corner. I like that the classic theme is not very distracting. Removing the lines allows the reader to view the data without noise created by grid lines. Additionally, I appreciate that the axes are clearly marked with bold lines which draws the reader’s attention to the scales.
Exercise 2
Using plot_01 from Exercise 1 and the theme() function, attempt to construct the ugliest plot possible (example pictured below). Be creative! It should NOT look exactly like the example. Since the goal is to understand a variety of adjustments, you should use a minimum of 10 different manual adjustments within theme().
We will be making use of your code from Exercise 3 from L07 Layers. Using the NU_admission_data.csv you created two separate plots derived from the single plot depicted in undergraduate-admissions-statistics.pdf. Style these plots so they follow a “Northwestern” theme. You are welcome to display the plots separately OR design a layout that displays both together (likely one stacked above the other).
Check out the following webpages to help create your Northwestern theme:
Use a free non-standard font from google for the title. Pick one that looks similar to a Northwestern font.
Note
I find this blog post to be extremely useful for adding fonts. Important packages for using non-standard fonts are showtext, extrafont, extrafontdb, and sysfonts. The last 3 generally just need to be installed (not loaded per session).
Challenge is optional for all students, but we recommend trying them out!
Using cdc_small dataset, re-create your own version inspired by the plot below.
Must haves:
Use two non-standard fonts (one for labeling the point and the other for the axes)
Use at least two colors (one for the added point, another for the rest of the points)
A curved arrow used to label the point
Using Bilbo Baggins’ responses below to the CDC BRSFF questions, add Bilbo’s data point to a scatterplot of weight by height.
genhlth - How would you rate your general health? fair
exerany - Have you exercised in the past month? 1=yes
hlthplan - Do you have some form of health coverage? 0=no
smoke100 - Have you smoked at least 100 cigarettes in your life time? 1=yes
height - height in inches: 46
weight - weight in pounds: 120
wtdesire - weight desired in pounds: 120
age - in years: 45
gender - m for males and f for females: m
Note
Adding non-standard fonts can be an adventure. I find this blog post to be extremely useful for adding fonts. Important packages for using non-standard fonts are showtext, extrafont, extrafontdb, and sysfonts. The last 3 generally just need to be installed (not loaded per session).
Hint:
Create a new dataset (maybe call it bilbo or bilbo_baggins) using either data.frame() (base R - example in book) or tibble() (tidyverse - see help documentation for the function). Make sure to use variable names that exactly match cdc’s variable names. We have provided the tidyverse approach.
Search google fonts to find some free fonts to use (can get free fonts from other locations)
Solution
Code
# build dataset for Bilbo Bagginsbilbo <-tibble(genhlth ="fair",exerany =1,hlthplan =0,smoke100 =1,height =46,weight =120,wtdesire =120,age =45,gender ="m")